﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using PPIDesktopClient.General;
using System.Net;

namespace PPIDesktopClient.Entities
{
    public class EBaseImage : EBasePosition
    {
        protected string m_ImageWebserviceFolder = "";
        protected string m_ImageWebFolder = "";
        protected string m_ImageLocalFolder = "";
        protected string m_key = "";

        #region private members
        private Int32 m_Id;
        private FileInfo m_File;
        private EBaseAsset m_Asset;
        private EBasePole m_Pole;
        private double m_direction;
        private string m_FileName;
        private bool m_IsDesasocciated;
        private bool m_IsDeleted;


        #endregion

        #region Properties
        public Int32 Id
        {
            get
            {
                return m_Id;
            }
            set
            {
                m_Id = value;
            }
        }
        public string FileName
        {
            get
            {
                return m_FileName;
            }
            set
            {
                m_FileName = value;
            }
        }
        public FileInfo File
        {
            get
            {
                return m_File;
            }
            set
            {
                m_File = value;
            }
        }
        public double Direction
        {
            get
            {
                return m_direction;
            }
            set
            {
                m_direction = value;
            }
        }
        public string Name
        {
            get { return m_File.Name; }
        }
        public EBaseAsset Asset
        {
            get
            {
                return m_Asset;
            }
            set
            {
                m_Asset = value;
            }
        }
        public EBasePole Pole
        {
            get
            {
                return m_Pole;
            }
            set
            {
                m_Pole = value;
            }
        }
        public bool IsDesasocciated
        {
            get
            {
                return m_IsDesasocciated;
            }
            set
            {
                m_IsDesasocciated = value;
            }
        }
        public bool IsDeleted
        {
            get
            {
                return m_IsDeleted;
            }
            set
            {
                m_IsDeleted = value;
            }
        }



        #endregion

        #region Constructors

        public EBaseImage()
        :base()
        {
            m_Id = Int32.MinValue;
            m_Pole = new EBasePole();
            m_Asset = new EBaseAsset();
            ConfigReader reader = ConfigReader.GetInstance();
            m_key = reader.GetConnectionString();
        }

        public EBaseImage(Int32 id)
        :this()
        {
            m_Id = id;
        }

        public EBaseImage(Int32 id, string filename)
        :this(id)
        { 
            m_FileName = filename;
        }

        #endregion

        #region Utilities
        public static byte[] ToBytes(string fileName)
        {
            FileStream _FileStream = null;
            BinaryReader _BinaryReader = null;
            try
            {
                _FileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                _BinaryReader = new BinaryReader(_FileStream);
                byte[] ImageBytes = _BinaryReader.ReadBytes((int)_FileStream.Length);
                return ImageBytes;

            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                _BinaryReader.Close();
                _FileStream.Close();
                _FileStream.Dispose();
            }
        }

        public static FileInfo GetImageFile(int structureID, string fileName)
        {
            string _DestinationImagePath;
            _DestinationImagePath = PPIDesktopClient.General.DirHelper.CreateVisitDirectory(structureID, PPIDesktopClient.General.Constants.K_ImageCachePath) + Path.DirectorySeparatorChar + fileName;
            return new FileInfo(_DestinationImagePath);
        }
        #endregion

        #region Virtual Methods

        public virtual int Save()
        {
            if (m_Id == Int32.MinValue)
            {
                m_Id = Insert();
            }
            return m_Id;
        }
        protected virtual int Insert()
        {
            throw new NotImplementedException();
        }
        public virtual void Delete()
        {
            throw new NotImplementedException();
        }
        public virtual void Disassociate()
        {
            throw new NotImplementedException();
        }
        public virtual void Associate(int poleID)
        {
            throw new NotImplementedException();
        }

        public virtual string GetHtml()
        {
            ConfigReader r = ConfigReader.GetInstance();
            int mode = r.GetConnectionMode();
            StringBuilder s = new StringBuilder();
            s.AppendLine("<a href=\"http://www.dev-poleposition.com/Dev/WebSite/ImagePreview.aspx?Mode=" + mode + "&w=0&h=0IdImage=" + m_Id + "\"><img src=\"" + m_File.FullName + "\" border=\"0\"/></a>");
            return s.ToString();
        }

        public virtual List<EBaseImage> GetServer(int offset)
        {
            throw new NotImplementedException();
        }

        public virtual EBaseAsset GetAsset()
        {
            return Asset;
        }
        public virtual byte[] GetImageBytes()
        {
            WebClient _Brownser = new WebClient();
            return _Brownser.DownloadData(GetWebFileUrl());
        }

        public virtual FileInfo GetLocalPath()
        {
            return new FileInfo(DirHelper.GetImageDirectory(m_ImageLocalFolder, Asset.Id, Id, true)); 
        }

        public virtual string GetWebserviceFile()
        {
            return DirHelper.GetImageDirectory(m_ImageWebserviceFolder, Asset.Id, Id, false);
        }

        public virtual string GetWebFileUrl()
        {
            return DirHelper.GetImageDirectory(m_ImageWebFolder, Asset.Id, Id, false);


        }

        #endregion

        #region Override


        protected override string GetPlaceMark()
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine("<Placemark id=\"" + GetPlacemarkId() + "\">");
            s.AppendLine(" <Style>");
            s.AppendLine("     <BalloonStyle>");
            s.AppendLine("         <color>F9DDCD</color>");
            s.AppendLine("         <text>$[description]</text>");
            s.AppendLine("     </BalloonStyle>");
            s.AppendLine("</Style>");
            s.AppendLine(" <description>");
            s.AppendLine("     <![CDATA[" + GetHtmlDescription() + "]]>");
            s.AppendLine(" </description>");
            s.AppendLine("</Placemark>");
            return s.ToString();
        }

        public override bool Equals(object obj)
        {
            if(obj is EBaseImage)
            {
                EBaseImage i = obj as EBaseImage;
                // Return true if the fields match:
                return (Id == i.Id);
            }else
            {
                return false;
            }
        }

        #endregion

    }
}
